package com.hui.account.utils;

import android.Manifest;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;
import android.content.ContentResolver;
import android.os.FileUtils;
import android.provider.OpenableColumns;
import android.text.TextUtils;

import androidx.annotation.RequiresApi;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import java.io.ByteArrayOutputStream;

public class ImageUtils {

    /**
     * Bitmap 转成 String
     */
    public static String forString(Bitmap bitmap) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        byte[] buffer = out.toByteArray();
        return Base64.encodeToString(buffer, Base64.DEFAULT);
    }

    /**
     * Bitmap 转成 byte[]
     */
    public static byte[] forByte(Bitmap bitmap) {
        // 将图片转化为位图
        int size = bitmap.getWidth() * bitmap.getHeight() * 4;
        // 创建一个字节数组输出流,流的大小为size
        ByteArrayOutputStream out = new ByteArrayOutputStream(size);
        try {
            // 设置位图的压缩格式，质量为100%，并放入字节数组输出流中
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            // 将字节数组输出流转化为字节数组byte[]
            return out.toByteArray();
        } catch (Exception e) {
            Log.e(CommonUtils.TAG, "ImageUtils forByte 异常信息:{}" + e.getMessage());
        } finally {
            try {
                bitmap.recycle();
                out.close();
            } catch (IOException e) {
                Log.e(CommonUtils.TAG, "ImageUtils forByte 异常信息:{}" + e.getMessage());
            }
        }
        return new byte[0];
    }

    /**
     * String 转成 Bitmap
     */
    public static Bitmap forBitmap(String value) {
        byte[] buffer = Base64.decode(value, Base64.DEFAULT);
        return BitmapFactory.decodeByteArray(buffer, 0, buffer.length);
    }

    /**
     * 查找文件
     */
    public static Bitmap getDiskBitmap(String path) {
        Bitmap bitmap = null;
        try {
            File file = new File(path);
            if (file.exists()) {
                bitmap = BitmapFactory.decodeFile(path);
            }
        } catch (Exception e) {
            return null;
        }
        return bitmap;
    }

    /**
     * 删除文件
     */
    public static void delDiskFile(String path) {
        if (TextUtils.isEmpty(path))
            return;

        File file = new File(path);
        if (file.exists() && file.isFile())
            file.delete();
    }

    /**
     * 保存bitmap到本地
     */
    public static void saveBitmap(Bitmap bitmap, String fileName) {
        String savePath = CommonUtils.IMAGES_PATH + fileName;
        try {
            File filePic = new File(savePath);
            if (!filePic.exists()) {
                filePic.getParentFile().mkdirs();
                filePic.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(filePic);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            Log.e(CommonUtils.TAG, "保存Bitmap：" + e.getMessage());
            return;
        }
    }
}
